package com.database.controller;

import com.alibaba.fastjson.support.retrofit.Retrofit2ConverterFactory;
import com.auth0.jwt.JWT;
import com.database.character.Result;
import com.database.character.UserAccount;
import com.database.service.UserService;
import com.database.utils.JwtUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(value = "用户controller",tags = {"用户相关接口"})
@RestController
@RequestMapping("/users")
public class UserController {
	@Autowired
	private UserService userService;
	private JwtUtils jwtUtils;
	//	注册
	@ApiResponses(value = {
			@ApiResponse(code = 200, message = "注册成功！", response = Result.class)
	})
	@ApiOperation(value = "注册",notes = "注册用户",httpMethod = "POST")
	@PostMapping("/register")
	public Result register(@RequestBody UserAccount userAccount){
		String nickname = userAccount.getNickname();
		UserAccount byNickname = userService.getByNickname(nickname);
		if(byNickname!=null){
			return Result.error("昵称重复！");
		}
		else{
			int i = userService.addUser(userAccount);
			if(i==1){
				return Result.success("注册成功！",null);
			}
			else{
				return Result.error("注册失败！请联系管理员！");
			}
		}
	}
	//登录
	@PostMapping("/login")
	@ApiOperation(value = "登录",notes = "登录用户",httpMethod = "POST")
	@ApiResponses(
			@ApiResponse(code = 200, message = "登录成功！", response = Result.class)
	)
	public Result login( @RequestBody UserAccount userAccount){
		String nickname = userAccount.getNickname();
		UserAccount byNickname = userService.getByNickname(nickname);
		if(byNickname==null){
			return Result.error("用户名不存在！");
		}
		else{
			if(byNickname.getPassword().equals(userAccount.getPassword())){
				Map<String,Object> map = new HashMap<>();
				map.put("nickname",nickname);
				map.put("password",userAccount.getPassword());
				map.put("id",byNickname.getUserId());
				String token = jwtUtils.generateJwt(map);
				Integer userId = byNickname.getUserId();
				Map map1 = new HashMap();
				map1.put("token",token);
				map1.put("userId",userId);
				map1.put("userType",byNickname.getUserType());
				return Result.success("登陆成功！",map1);
			}
			else{
				return Result.error("密码错误！");
			}
		}
	}
	//更新用户信息
	@ApiOperation(value = "更新用户信息",notes = "更新用户信息",httpMethod = "PUT")
	@ApiResponses(
			@ApiResponse(code = 200, message = "更新成功！", response = Result.class)
	)
	@ApiImplicitParam(name = "token",value = "token",required = true,dataType = "String",paramType = "header")
	@PutMapping
	public Result updateUser(@RequestBody UserAccount userAccount){
		Integer userId = userAccount.getUserId();

		int i = userService.updateUser(userAccount);
		if(i==0){
			return Result.error("修改个人信息失败，请联系管理员！");
		}
		else{
			UserAccount byUid = userService.getByUid(userId);
			return Result.success("修改成功",byUid);
		}
	}
	//根据id删除
	@DeleteMapping("/{userId}")
	@ApiOperation(value = "根据id删除用户",notes = "根据id删除用户",httpMethod = "DELETE")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "token",value = "token",required = true,dataType = "String",paramType = "header")
			,@ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "Integer",paramType = "path")
	})
	@ApiResponses(
			@ApiResponse(code = 200, message = "删除成功！", response = Result.class)
	)
	public Result deleteUser(@PathVariable Integer userId){
		int i = userService.deleteUserByUid(userId);
		if(i==0){
			return Result.error("删除失败！");
		}
		else{
			return Result.success("删除成功！",null);
		}
	}
	//根据id查询
	@PostMapping("/findUId")
	@ApiOperation(value = "根据id查询用户",notes = "根据id查询用户",httpMethod = "POST")
	@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	@ApiImplicitParam(name = "token",value = "token",required = true,dataType = "String",paramType = "header")
	public Result getUser(@RequestHeader("token") String token){
		Integer userId = (Integer) jwtUtils.parseJWT(token).get("id");
		System.out.println(userId);
		UserAccount byUid = userService.getByUid(userId);
		if(byUid==null){
			return Result.error("查询失败，输入的用户id不存在！");
		}
		else{
			return Result.success("查询成功！",byUid);
		}
	}

	@GetMapping("/{userId}")
	@ApiOperation(value = "根据id查询用户",notes = "根据id查询用户",httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "userId",value = "用户id",required = true,dataType = "Integer",paramType = "path"),
			@ApiImplicitParam(name = "token",value = "token",required = true,dataType = "String",paramType = "header")
	})
	@ApiResponses(
			@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	)
	public Result getUser(@PathVariable Integer userId){
		UserAccount byUid = userService.getByUid(userId);
		if(byUid==null){
			return Result.error("查询失败，输入的用户id不存在！");
		}
		else{
			return Result.success("查询成功！",byUid);
		}
	}
	//获得所有用户
	@GetMapping
	@ApiOperation(value = "获得所有用户",notes = "获得所有用户",httpMethod = "GET")
	@ApiResponses(
			@ApiResponse(code = 200, message = "查询成功！", response = Result.class)
	)
	public Result getAll(){
		List<UserAccount> list = userService.list();
		return Result.success("查询成功！",list);
	}
}
